/*----------------------------------------------------------------------------*\
| /gamemode/cl_chat.lua                | chat system                     | CLI |
\*----------------------------------------------------------------------------*/

include("shd_chat.lua")

// Define chat colors in a local table so we can look it up
local ChatColors = {}
ChatColors[CHAT_COL_DEF]       = Color(255,255,255,255)
ChatColors[CHAT_COL_CHAT]      = Color(220,220,220,255)
ChatColors[CHAT_COL_SYS]       = Color(255,255,  0,255)
ChatColors[CHAT_COL_KILL]      = Color(200, 16,  8,255)
ChatColors[CHAT_COL_WARN]      = Color(200,140, 32,255)
ChatColors[CHAT_COL_ACT]       = Color(255,255,  0,255)
ChatColors[CHAT_COL_HELP]      = Color(130,203, 39,255)

// Yes, local variables are more effective than local tables
local ShowChatbox = false
local FadeTimer = CurTime()
local CurrentText = ""
local Buffer = {}
local CurrentAlpha = 0

local function CreateFonts()
	// Create the chat font
	surface.CreateFont( "Tahoma", 16, 600, false, false, "ZDChatFont" )
end
hook.Add("Initialize", "CreateFonts", function() CreateFonts(); end)
	

function GM:StartChat( teamsay )
	ShowChatbox = true
	return true
end

function GM:FinishChat()
	ShowChatbox = false
	CurrentText = ""
end

function GM:ChatTextChanged( text )
	CurrentText = text
end

local function DoChat( id, name, text, filter, color_enum )
	print("Filter: " .. filter )	
	
	if ( id > 0 ) then
		print( name .. ": " .. text )
	else
		print( "System: " .. text )
	end
	
	// Lookup the color if it's in the translation table ( i.e. it's an ENUM )
	if ( ChatColors[color_enum] ) then
		color_enum = ChatColors[color_enum]
	end
	
	local tab = {}
	tab.id = id
	tab.name = name
	tab.text = text
	tab.filter = filter
	tab.IsFading = false
	tab.color = color_enum or CHAT_COL_CHAT and Error("ChatSystem - DoChat: Could not find the color!\n")

	table.insert( Buffer, 1, tab )
	FadeTimer = CurTime() + 10

end

function SendLocalMessage( id, name, text, filter, color_enum )
	DoChat( id, name, text, filter, color_enum )
end

function GM:ChatText( id, name, text )
	DoChat( id, name, text, "chat", CHAT_COL_CHAT )
	return true
end

local function RecvTextNotice( um )
	local tab = {}
	tab.id = 0
	tab.name = ""
	tab.text = um:ReadString()
	tab.filter = nil
	tab.IsFading = false
	tab.color = um:ReadShort() or CHAT_COL_CHAT

	table.insert( Buffer, 1, tab )
	FadeTimer = CurTime() + 10
end
usermessage.Hook( "RetriveChatMessage", function(um) RecvTextNotice(um); end )

local function ChatDraw()
	// Should we draw the chat buffer?
	if (ShowChatbox or CurrentAlpha > 0) then
	
		// Define outline color
		local OutlineColor = Color(0,0,0,ChatColors[CHAT_COL_DEF].a)

		// Draw everything in the buffer
		for k,tab in ipairs(Buffer) do

			// Localize table values
			local id		= tab.id or 0
			local name 		= tab.name
			local text 		= tab.text or "" and Error("ChatSystem: Could not find any text!")
			local filter 	= tab.filter or ""
			local IsFading 	= tab.IsFading or false
			local color 	= tab.color or Error("ChatSystem: Could not find color\n")
			
			// If there is a name, add it to the text
			if ( name && id > 0 ) then
				text = name .. ": " .. text
			end
			
			// Lookup the color if it's in the translation table ( i.e. it's an ENUM )
			if ( ChatColors[color] ) then
				color = ChatColors[color]
			end
			
			if (!color or !color.r or !color.b or !color.g or !color.a) then
				color = ChatColors[CHAT_COL_CHAT]
			end
			
			// If it's fading, cut the color in two
			if ( IsFading ) then
				color = Color(color.r/2,color.g/2,color.b/2,CurrentAlpha)
			end
			
			// If we're showing the chatbox, full alpha else normal alpha
			if (ShowChatbox) then
				color.a = 255
				OutlineColor.a = 255
			else
				color.a	= CurrentAlpha
				OutlineColor.a = CurrentAlpha
			end
			
			// The actual drawing			
			draw.SimpleTextOutlined(text, "ZDChatFont", 5, ScrH()-102-(k*14), color, 0, 4, 1, OutlineColor)
		end

	end
	
	// Draw the player's own chatline?
	if (ShowChatbox) then
	
		local prefix = ">"
		local ChatString = CurrentText
	
		// Should we use command prefix?
		if ( string.sub(CurrentText,1,1)=="/" or IsACommand ) then
			ChatString	= string.sub(CurrentText,2)
			prefix	= "$"
		end
		
		// draw it !
		draw.SimpleTextOutlined(prefix .. " " .. ChatString,"ZDChatFont", 2, ScrH()-102, Color(255,255,255,255), 0, 4,1, Color(0,0,0,255))
	end
	
end
hook.Add("HUDPaint", "ChatDraw", function() ChatDraw(); end)

local function ChatCycle()
	while (#Buffer > 6) do
		table.remove( Buffer )
	end
	
	if (FadeTimer <= CurTime()) then
		CurrentAlpha = math.Approach( CurrentAlpha, 0,	-2 )
	else
		CurrentAlpha = math.Approach( CurrentAlpha, 255,	16 )
	end
	
	if (CurrentAlpha == 0) then
		for k, tab in ipairs( Buffer ) do
			tab.IsFading = true
		end
	end
end
hook.Add("Think", "ChatCycle", function() ChatCycle(); end)



